import pandas as pd
import pygame

from data.level_data import LevelData
from scenes.base_scene import Scene


class CloseLevelScene(Scene):
    def __init__(self, screen, level_data:LevelData):
        super().__init__()
        self.level_data = level_data
        self.data = None
        self.font = pygame.font.SysFont("方正粗黑宋简体", 24)
        self.text_color = (255, 255, 255)
        self.screen = screen
        self.running = True
        self.current_index = 0
        self.show_next_level_button = False

    def load_assets(self):
        df = self.level_data.get_task_people()
        cash_current = self.level_data.calc_cash(df)
        me = self.level_data.get_me_info()
        self.data = cash_current.loc[me['name'].values[0]].astype(int).to_dict()

    def handle_events(self, event):
        if event.type == pygame.KEYDOWN:
            if self.show_next_level_button:
                pass
            else:
                return "game_over"

    def update(self):
        # 检查是否破产
        if self.data['cash_retention'] < 0:
            self.show_next_level_button = False
        else:
            self.show_next_level_button = True

    def draw(self):
        self.screen.fill((0, 0, 0))
        lines = [
            ("项目结算", f"项目总收入：{self.data['bonus']}"),
            ("", f"任务收入：{self.data['bonus_task']}"),
            ("", f"薪水收入：{self.data['salary_earnings']}"),
            ("-" * 20, "-" * 20),
            ("惩罚", f"项目超时：{self.data['project_overdue']}"),
            ("", f"任务惩罚：{self.data['task_penalty']}"),
            ("", f"体力惩罚：{self.data['physical_penalty']}"),
            ("-" * 20, "-" * 20),
            ("日常开销", f"房租：{-self.data['rent_expense']}"),
            ("", f"生活费：{-self.data['living_expense']}"),
            ("", f"社交：{-self.data['social_expense']}"),
            ("-" * 20, "-" * 20),
            ("结算", f"现金：{self.data['cash_retention']}"),
        ]

        # 计算屏幕中轴线
        screen_center_x = self.screen.get_width() // 2
        screen_center_y = self.screen.get_height() // 3
        text_height = len(lines) * 40 // 2

        y_offset = 30
        for line in lines:
            if line[0]:
                text = self.font.render(line[0], True, self.text_color)
                text_rect = text.get_rect(center=(screen_center_x, y_offset))
                self.screen.blit(text, text_rect)
                y_offset += 40
            text = self.font.render(line[1], True, self.text_color)
            text_rect = text.get_rect(center=(screen_center_x, y_offset))
            self.screen.blit(text, text_rect)
            y_offset += 30

        # 判断破产
        if self.data['cash_retention'] < 0:
            bankruptcy_text = self.font.render("你破产了", True, self.text_color)
            bankruptcy_rect = bankruptcy_text.get_rect(center=(screen_center_x, self.screen.get_height() - 60))
            self.screen.blit(bankruptcy_text, bankruptcy_rect)
        elif self.show_next_level_button:
            next_level_text = self.font.render("下一关", True, self.text_color)
            next_level_rect = next_level_text.get_rect(center=(screen_center_x, self.screen.get_height() - 60))
            self.screen.blit(next_level_text, next_level_rect)

        pygame.display.flip()



if __name__ == '__main__':
    # 假设有一个DataFrame作为输入数据
    pygame.init()
    # 假设有一个DataFrame作为输入数据
    data = {
        'project_bonus': 5000,
        'task_bonus': 3000,
        'salaray_income': 2000,
        'project_overdue': 100,
        'task_penalty': 50,
        'energy_penalty': 20,
        'rent_expense': 5000,
        'living_expense': 3000,
        'social_expense': 1000,
        'new_cash': 3000,
        'cash': 5000,
        'cash_change': 2000
    }
    df = pd.DataFrame(data, index=[0])

    # 创建FinancialScene实例并运行
    financial_scene = CloseLevelScene(df.iloc[0])
    financial_scene.run()